//
// Created by 32827 on 2024/2/6.
//
#include "yzf_tim.h"

YZF_RESULT yzf_tim_open_clock(YZF_TIM_POINTER ptr){
    switch (ptr) {
        case TIMER0:
            rcu_periph_clock_enable(RCU_TIMER0);
            return YZF_OK;
        case TIMER1:
            rcu_periph_clock_enable(RCU_TIMER1);
            return YZF_OK;
        case TIMER2:
            rcu_periph_clock_enable(RCU_TIMER2);
            return YZF_OK;
        case TIMER3:
            rcu_periph_clock_enable(RCU_TIMER3);
            return YZF_OK;
        case TIMER4:
            rcu_periph_clock_enable(RCU_TIMER4);
            return YZF_OK;
        case TIMER5:
            rcu_periph_clock_enable(RCU_TIMER5);
            return YZF_OK;
        case TIMER6:
            rcu_periph_clock_enable(RCU_TIMER6);
            return YZF_OK;
        case TIMER7:
            rcu_periph_clock_enable(RCU_TIMER7);
            return YZF_OK;
        case TIMER8:
            rcu_periph_clock_enable(RCU_TIMER8);
            return  YZF_OK;
        case TIMER9:
            rcu_periph_clock_enable(RCU_TIMER9);
            return YZF_OK;
        case TIMER10:
            rcu_periph_clock_enable(RCU_TIMER10);
            return YZF_OK;
        case TIMER11:
            rcu_periph_clock_enable(RCU_TIMER11);
            return YZF_OK;
        case TIMER12:
            rcu_periph_clock_enable(RCU_TIMER12);
            return YZF_OK;
        case TIMER13:
            rcu_periph_clock_enable(RCU_TIMER13);
            return YZF_OK;
        default:
            return YZF_NOT_FOUND;
    }
}
YZF_RESULT yzf_tim_enable(YZF_TIM_POINTER ptr){
    TIMER_CTL0(ptr) |= TIMER_CTL0_CEN;
    return YZF_OK;
}
YZF_RESULT yzf_tim_disable(YZF_TIM_POINTER ptr){
    TIMER_CTL0(ptr)&= ~TIMER_CTL0_CEN;
   return YZF_OK;
}
/**
 * @param div 00: 1;01: 2;10: 4;
 * */
YZF_RESULT yzf_tim_clock_div(YZF_TIM_POINTER ptr,uint16_t div){
    TIMER_CTL0(ptr) = TIMER_CTL0(ptr)&~TIMER_CTL0_CKDIV | (div<<8)&TIMER_CTL0_CKDIV;
    return YZF_OK;
}
YZF_RESULT yzf_tim_set_dir(YZF_TIM_POINTER ptr,YZF_BOOL isUP){
    if(!isUP){
        TIMER_CTL0(ptr) |= TIMER_CTL0_DIR;
    }else{
        TIMER_CTL0(ptr) &= ~TIMER_CTL0_DIR;
    }
    return YZF_OK;
}
//更新中断
YZF_RESULT yzf_tim_int_UP(YZF_TIM_POINTER ptr,YZF_BOOL isEnable){
    if(isEnable){
        TIMER_DMAINTEN(ptr) |=  TIMER_INT_UP;
    }else{
        TIMER_DMAINTEN(ptr) &=  ~TIMER_INT_UP;
    }
    return YZF_OK;
}
YZF_BOOL yzf_tim_flag_upie(YZF_TIM_POINTER ptr){
    return (TIMER_INTF(ptr) & TIMER_INT_FLAG_UP) !=0 ? YZF_TRUE:YZF_FALSE;
}
YZF_RESULT yzf_tim_flag_clear_upie(YZF_TIM_POINTER ptr){
    TIMER_INTF(ptr) &= ~TIMER_INT_FLAG_UP;
    return  YZF_OK;
}

uint32_t yzf_tim_get_count(YZF_TIM_POINTER ptr){
    return TIMER_CNT(ptr);
}
YZF_RESULT yzf_tim_set_count(YZF_TIM_POINTER ptr,uint32_t count){
    TIMER_CNT(ptr) = count;
    return YZF_OK;
}
//重新装载
YZF_RESULT yzf_tim_set_period(YZF_TIM_POINTER ptr,uint32_t period){
    TIMER_CAR(ptr) = period;
    return YZF_OK;
}
//预分频
YZF_RESULT yzf_tim_set_prescaler(YZF_TIM_POINTER ptr,uint16_t prescaler){
    TIMER_PSC(ptr) = prescaler;
    return YZF_OK;
}